/**
 * Common.js
 * Author: Feil.Wang
 * Date: 2013-9-23
 */


$(function () {

    windowEffect();

    $("#keyword").live("focus",function () {
        if ($(this).val() == this.defaultValue) {
            $(this).val("");
        }
    }).live("blur", function () {
            if ($(this).val() == "") {
                $(this).val(this.defaultValue);
            }
        });

    if($('#inputEmail')[0]){
        placeholder("inputEmail","emailPlaceholder");
    }
    if($('#inputPwd')[0]){
        placeholder("inputPwd","pwdPlaceholder");
    }

    //select file type
    $("#fileType").click(function(){
        var list = $(this).children("ul");
        if(!list.hasClass("show")){
            list.slideDown(200);
            list.addClass("show");
        }else{
            list.slideUp(200);
            list.removeClass("show");
        }
    });
    $("#fileType li").click(function(){
        var typeStr = $(this).text();
        $("#fileType .cur-type").text(typeStr);
    });
});

/**
 * 初始页面窗口动画效果
 */
var windowEffect = function () {
    $(".dialog-wrap-a").animate({
        "top": "60px",
        "opacity":1
    }, 500);

    $("#index .item-1").animate({
        "left":"80px",
        "top":"150px"
    },500);
    $("#index .item-2").animate({
        "left":"205px",
        "top":"150px"
    },500);
    $("#index .item-3").animate({
        "left":"80px",
        "top":"275px"
    },500);
    $("#index .item-4").animate({
        "left":"330px",
        "top":"150px"
    },500);
    $("#index .item-5").animate({
        "left":"205px",
        "top":"275px"
    },500);
    setTimeout(function(){
        $(".player").animate({
            "left":"600px",
            "top":"150px"
        },500);
    },500);
    $(".hall-list-btn").animate({
        "top":"100px"
    },500);


    $(".chat-notice").animate({
        "left":"50px"
    },500);

    $(".booth-player").animate({
        "left":"600px"
    },500);

    $("#boothIndex .item-6").animate({
        "left":"470px",
        "top":"370px"
    },500);
    $("#boothIndex .item-7").animate({
        "left":"640px",
        "top":"370px"
    },500);
    $("#boothIndex .item-3").animate({
        "left":"805px",
        "top":"370px"
    },500);

    $("#dialogEffect").animate({
        "top":"45px",
        "opacity":1
    },500);

}

/**
 * 输入框提示信息
 * @param objid
 * @param objid2
 */
function placeholder(objid,objid2){
    var fos = document.getElementById(objid);
    if(fos.value!=''){
        $("#"+objid2).hide();
    }
    fos.onfocus = function(){
        $("#"+objid2).addClass("focus");
    }
    fos.onblur = function(){
        $("#"+objid2).removeClass("focus");
    }
    var element = document.getElementById(objid);
    if("\v"=="v"){
        element.onpropertychange = webChange;
    }else{
        element.addEventListener("input",webChange,false);
    }
    function webChange(){
        if(element.value){
            $("#"+objid2).hide();
        }else if(element.value==""){
            $("#"+objid2).show();
        }
    }
}


/**
 * 弹窗操作
 */
(function(window,undefined){

    var preventReClick = true; //设置防止重复点击全局变量

    window.hdialog = {
        //载入对话框
        load : function(url,callback){	//第三个参数为“globalMiddle”时，dialog全局居中
            if(!preventReClick) return;
            preventReClick = false;

            $(".container").append('<div class="dialog-container"></div>');
            $targetDialog = $(".dialog-container");
            $targetDialog.load(url,function(responseTxt,statusTxt,xhr){
                if(statusTxt=="success"){
                    var w = $targetDialog.outerWidth(),
                        h = $targetDialog.outerHeight() + 40;
                    $targetDialog.css({
                        "margin-left":"-"+w/2+"px",
                        "margin-top":"-"+h/2+"px"
                    });
                    hdialog.show(callback);
                }else if(statusTxt=="error"){
                    alert("加载失败");
                    $targetDialog.remove();
                }
            });
        },
        //重载另一个页面
        reload: function(url,callback){
            hdialog.close(function(){
                hdialog.load(url,callback);
            });
        },
        //显示对话框
        show : function(callback){
            $(".overlay").fadeTo(300,0.3);
            $targetDialog.show(300,callback);
            preventReClick = true;
            $(".dialog-title .close").click(function(){
                hdialog.close();
            });
            $("#v_frame").hide();
        },
        //关闭对话框
        close : function(callback){
            $targetDialog.hide(300,function(){
                $targetDialog.remove();
                if(!!callback){callback()}
            });
            if(!callback){
                $(".overlay,.header-overlay").fadeOut(300);
                $("#v_frame").show();
            }
        }
    }

})(window);

